//
//  ZYXPictruesViewController.m
//  OCUIBasis
//  图片浏览
//

#define ZYXIconKey @"icon"
#define ZYXDescKey @"desc"

#import "ZYXPictruesViewController.h"

@interface ZYXPictruesViewController ()

- (IBAction)previous;
- (IBAction)next;


@property (weak, nonatomic) IBOutlet UILabel *noLabel;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;


@property (weak, nonatomic) IBOutlet UIImageView *iconView;

@property (weak, nonatomic) IBOutlet UIButton *previousBtn;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;

/**
 *  当前显示图片
 */
@property (assign,nonatomic)int index;

/**
 *  图片数组
 *  strong 一般对象
 *  weak   UI控件
 */
@property (strong,nonatomic)NSArray *imageArray;

@end

@implementation ZYXPictruesViewController

/*
-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.index = 0;
    [self changeData1];
    
    //self.iconView.image = [UIImage imageNamed:@"biaoqingdi"];
    //self.previousBtn.enabled = NO;
    //self.nextBtn.enabled = NO;
}
*/

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.index = 0;
    [self changeData2];
}


- (IBAction)previous {
    self.index --;
    //[self changeData1];
    
    [self changeData2];
}

- (IBAction)next {
    self.index ++;
    //[self changeData1];
    
    [self changeData2];
}

/**
 *  数据在代码中写死
 */
/*
-(NSArray *)imageArray
{
    if(_imageArray == nil){
        //1.初始化5组图片数据
        NSMutableDictionary *image1 = [NSMutableDictionary dictionary];
        image1[ZYXIconKey] = @"biaoqingdi";
        image1[ZYXDescKey] = @"在他面前，其他神马表情都弱爆了！";
        
        NSMutableDictionary *image2 = [NSMutableDictionary dictionary];
        image2[ZYXIconKey] = @"bingli";
        image2[ZYXDescKey] = @"这也忒狠了";
        
        NSMutableDictionary *image3 = [NSMutableDictionary dictionary];
        image3[ZYXIconKey] = @"chiniupa";
        image3[ZYXDescKey] = @"这小姑娘吃个牛排比杀牛还费劲啊";
        
        NSMutableDictionary *image4 = [NSMutableDictionary dictionary];
        image4[ZYXIconKey] = @"danteng";
        image4[ZYXDescKey] = @"亲，你能改下你的网名么？哈哈";
        
        NSMutableDictionary *image5 = [NSMutableDictionary dictionary];
        image5[ZYXIconKey] = @"wangba";
        image5[ZYXDescKey] = @"哥们为什么选八号呢";
        
        //2.把图片数据封装到对象的属性中
        _imageArray = @[image1,image2,image3,image4,image5];
        
    }
    return _imageArray;
}
*/

/**
 *  从plist文件中读取数据
 *
 *  @return 图片数据
 */
-(NSArray *)imageArray
{
    if(_imageArray == nil){
        
        // 初始化数据
        // File : 全路径
        // NSBundle : 一个NSBundle代表一个文件夹
        
        // 利用mainBundle就可以访问软件资源包中的任何资源
        
        //1.获取plist的全路径
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *PictruesPath = [bundle pathForResource:@"Pictrues" ofType:@"plist"];
        
        //2.通过plist文件加载数组
        _imageArray = [NSArray arrayWithContentsOfFile:PictruesPath];
    }
    return _imageArray;
}


-(void)changeData2
{
    self.noLabel.text = [NSString stringWithFormat:@"%d/%lu",self.index + 1,(unsigned long)self.imageArray.count];
    
    NSDictionary *imgDict = self.imageArray[self.index];
    self.descLabel.text = imgDict[ZYXDescKey];
    self.iconView.image = [UIImage imageNamed:imgDict[ZYXIconKey]];
    
    //2.改变切换按钮的状态
    self.previousBtn.enabled = (self.index != 0);
    self.nextBtn.enabled = (self.index != self.imageArray.count-1);
    
}

/**
 *  把数据写在代码里 没有扩展性
 */
-(void)changeData1
{
    //1.改变数据
    switch(self.index){
            
        case 0:
            self.noLabel.text = @"1/5";
            self.iconView.image = [UIImage imageNamed:@"biaoqingdi"];
            //要想让UILabel自动换行，设置Lines为0即可
            self.descLabel.text = @"在他面前，其他神马表情都弱爆了！";
            break;
        
        case 1:
            self.noLabel.text = @"2/5";
            self.iconView.image = [UIImage imageNamed:@"bingli"];
            self.descLabel.text = @"这也忒狠了";
            break;
            
        case 2:
            self.noLabel.text = @"3/5";
            self.iconView.image = [UIImage imageNamed:@"chiniupa"];
            self.descLabel.text = @"这小姑娘吃个牛排比杀牛还费劲啊";
            break;
            
        case 3:
            self.noLabel.text = @"4/5";
            self.iconView.image = [UIImage imageNamed:@"danteng"];
            self.descLabel.text = @"亲，你能改下你的网名么？哈哈";
            break;
            
        case 4:
            self.noLabel.text = @"5/5";
            self.iconView.image = [UIImage imageNamed:@"wangba"];
            self.descLabel.text = @"哥们为什么选八号呢";
            break;
        
    }
    //2.改变切换按钮的状态
    self.previousBtn.enabled = (self.index != 0);
    self.nextBtn.enabled = (self.index != 4);
}
@end
